begintownscript;

variables;
short creature_count = 0;
short choice = 0;
short pc_counter = 0;
short walk_dist = 0;
short flame_count = 0;
short dam_mod = 0;

body;

beginstate 0; //INIT_STATE
	set_flag(11,1,0);
	message_dialog("As you chase after Kharprev, you realize that the smoke in the air is slowly choking you, and that chasing him in the air just isn't going to work.","You land where he was perched before, and try to regain your breath.");
	set_state_continue(10);
break;

beginstate 1; //EXIT_STATE

break;

beginstate 2; //START_STATE
	if(get_flag(11,1) == 10) {
		play_sound(165);
		message_dialog("You hear an enormous magical explosion to the south, back where the holding cells were. You only hope that it's Brother Oakleaf and Ephesos.","If it is, then it's only a matter of time until you're saved.");
		}

	if(get_flag(11,1) != 0) {
		if(get_flag(11,1) == 11) {
			set_state_continue(30);
			}
		set_state_continue(20);
		}
break;

beginstate 10; //Walk up & let the endurance test begin!

	//walk up
	force_instant_terrain_redraw();
	pause(15);
	message_dialog("Once you can breathe again, you take a look around.  The floor is warm volcanic rock, lava and smoke surround everything, and it smells unbearably like lizard." ,"In an attempt to find Kharprev again, you silently walk toward the center of this strange structure...");
	walk_dist = group_dist_to_loc(0,16,17) - 1;
	while(walk_dist != 0) {
		march_party(16, (17 + walk_dist));
		force_view_center(16, (17 + walk_dist));
		force_instant_terrain_redraw();
		walk_dist = walk_dist - 1;
		pause(5);
	}
	march_party(16, 17);
	force_view_center(16, 17);
	force_instant_terrain_redraw();
	
	//enter the dragon
	pause(20);
	message_dialog("As you scan the smoke-filled skies, you realize that it's gotten too quiet. Sadly, there's little you can do when Kharprev drops out of the sky right in front of you.","");
	put_effect_on_space(16,15,1,15,0);
	put_effect_on_space(15,15,1,15,0);
	put_effect_on_space(17,15,1,15,0);
	put_effect_on_space(16,16,1,15,0);
	put_effect_on_space(16,14,1,15,0);
	run_animation_sound(10);
	activate_hidden_group(5);
	force_instant_terrain_redraw();
	pause(15);

	reset_dialog();
	add_dialog_str(0, "The dragon glares at you, realizing that you're actually serious about fighting. He laughs again, knocking you to the ground, and nearly throwing you into the lava below.",0);
	add_dialog_str(1, "Picking yourselves up off of the ground, you realize that you can't find the Skyshard! You curse Brother Oakleaf's planning again, realizing that you can't escape.", 0);
	add_dialog_str(2, "Kharprev bellows, _LOST YOUR PRECIOUS LITTLE TOY, HAVEN'T YOU? WHAT A PITY... I WAS HOPING TO TAKE THAT FOR MYSELF. I GUESS I'LL JUST HAVE TO BE CONTENT WITH KILLING YOU!!_",0);
	add_dialog_str(3, "Kharprev flaps his wings and rises into the air again. You can't guess what he's going to do, you just hope that Brother Oakleaf and Ephesos arrive soon.",0);
	run_dialog(1);
	take_all_of_item(448); //take Skyshard(s) away

	//exit the dragon
	pause(5);
	put_effect_on_space(16,15,1,15,1);
	put_effect_on_space(15,15,1,15,1);
	put_effect_on_space(17,15,1,15,1);
	put_effect_on_space(16,16,1,15,1);
	put_effect_on_space(16,14,1,15,1);
	run_animation_sound(10);
	erase_char(6);
	force_instant_terrain_redraw();

	pause(5);
	message_dialog("As Kharprev soars into the air, he begins dropping clouds of fire on you! You can't see any way to hit him, though the smoke is too dense to aim anyway.","Jumping down isn't even an option, so you're left to wait out the firestorm until Brother Oakleaf and Ephesos arrive.");

	set_flag(11,1,1); //start the countdown


break;

beginstate 20; //Kharprev's attacks... 10 turns of it.
	if(get_flag(11,1) < 11) {
		dam_mod = get_flag(11,1);
		//1st turn flame blanket 8-24
		if(get_flag(11,1) == 1){
			flame_count = 0;
			play_sound(167);
			message_dialog("Kharprev breathes a line of fire straight down the middle of the platforms, directly into you! Amazingly, it doesn't burn you on contact, though it leaves pools of fire on the ground.","");
			while (flame_count < 18) {
				put_field_on_space(16, (flame_count + 8), 1);
				flame_count = flame_count + 1;
				}
			}

		//6th turn flame blanket 8-24
		else if(get_flag(11,1) == 6) {
			flame_count = 0;
			play_sound(167);
			message_dialog("Kharprev breathes another line of fire straight down the middle of the platforms, down a different direction this time. Again, the floor is covered with pools of fire.","");
			while (flame_count < 18) {
				put_field_on_space((flame_count + 8), 16, 1);
				flame_count = flame_count + 1;
				}
			}

		//4th & 8th turn slashings
		else if((get_flag(11,1) == 4) || (get_flag(11,1) == 8)) {
			play_sound(103);
			message_dialog("Kharprev sweeps down toward you, slashing each of you with his claws. He disappears into the smoke before you can react.","");
			damage_char(1000, (7 * dam_mod), 4);
			}

		//clouds of flame on every other turn
		else {
			//Burn everyone
			print_str_color("Kharprev casts flames down onto you!",1);
			play_sound(167);
			pc_counter = 0;
			while (pc_counter < 4) {
				put_effect_on_char(pc_counter, 1,7,0);	
				pc_counter = pc_counter + 1;
				}
			run_animation_sound(51);
			damage_char(1000,(7 * dam_mod),1);
			}
		inc_flag(11,1,1);
		}

break;

beginstate 30; //successfully endured

	//re-enter the dragon
	set_total_visibility(1);
	play_sound(103);
	pause(5);
	force_view_center(16,15);
	force_instant_terrain_redraw();
	put_effect_on_space(16,15,1,15,0);
	put_effect_on_space(15,15,1,15,0);
	put_effect_on_space(17,15,1,15,0);
	put_effect_on_space(16,16,1,15,0);
	put_effect_on_space(16,14,1,15,0);
	run_animation_sound(10);
	activate_hidden_group(5);
	force_instant_terrain_redraw();
	pause(15);

	message_dialog("Kharprev descends in front of you again, looking slightly bored as the cloud of flames around him settles.","_I SEE WE'RE GOING TO HAVE TO DO THIS THE HARD WAY..._");
	set_flag(11,1,0);
	pause(10);
	
	//the hard way
	set_character_pose(6,1);
	force_instant_terrain_redraw();	
	play_sound(103);
	put_effect_on_space(16,15, 3, 6, 1);
	run_animation_sound(104);
	pause(3);
	put_effect_on_space(16,15, 1, 10, 1);
	run_animation_sound(104);
	pause(3);
	put_effect_on_space(16,15, 12, 14, 1);
	run_animation_sound(104);
	pause(3);
	pc_counter = 0;
	while(pc_counter < 4) {
		if(char_ok(0) == 1){
			put_jagged_zap(16,15,char_loc_x(pc_counter), char_loc_y(pc_counter), 0);
			put_boom_on_char(pc_counter, 1,0);
			}
		pc_counter = pc_counter + 1;
		}
	run_animation_sound(74);

	pause(10);
	set_character_pose(6,0);
	force_instant_terrain_redraw();
	pause(5);
	text_bubble_on_char(6, "YOU SHOULD BE DEAD...");
	force_instant_terrain_redraw();
	pause(20);
	erase_text_bubbles();
	text_bubble_on_char(6, "SOMETHING IS WRONG...");
	force_instant_terrain_redraw();
	pause(15);
	force_view_center(16,11);
	erase_text_bubbles();
	force_instant_terrain_redraw();
	put_boom_on_space(16,11,2,0);
	run_animation_sound(95);
	activate_hidden_group(10);
	set_character_facing(6,0);
	force_instant_terrain_redraw();
	pause(5);
	text_bubble_on_char(7, "I have arrived, Kharprev.");
	force_instant_terrain_redraw();
	pause(15);
	reset_dialog();
	add_dialog_str(0, "Ephesos calmly faces down Kharprev, smiling with a vague hint of punishment to come. Kharprev is visibly shaken.",0);
	add_dialog_str(1, "_YOU!! HOW DID YOU ESCAPE?! MY CAGE SPELLS CANNOT BE BROKEN!!!_ Kharprev bellows, a hint of fear creeping into his voice.",0);
	add_dialog_str(2, "_Always one for melodrama, weren't you? I think you should know that you cannot kill one of our Order while I walk free. Your flames will carry weight no longer._",0);
	add_dialog_str(3, "_NO!! YOU SHALL PERISH!!_ Kharprev screams, shaking the platform you stand on. The dragon begins to cast another spell, this time aiming at Ephesos...",0);
	run_dialog(1);
	erase_text_bubbles();
	set_character_pose(6,1);
	force_instant_terrain_redraw();
	pause(5);
	put_effect_on_space(16,15, 3, 6, 1);
	put_boom_on_char(7, 3,0);
	put_jagged_zap(16,15,16,11, 0);
	put_jagged_zap(16,11,0,2,0);
	run_animation_sound(104);
	pause(3);
	put_effect_on_space(16,15, 1, 10, 1);
	put_jagged_zap(16,15,16,11, 0);
	put_jagged_zap(16,11,0,31,0);
	put_boom_on_char(7, 3,0);
	run_animation_sound(104);
	pause(3);
	put_effect_on_space(16,15, 12, 14, 1);
	put_jagged_zap(16,15,16,11, 0);
	put_jagged_zap(16,11,31,0,0);
	put_boom_on_char(7, 3,0);
	run_animation_sound(104);
	pause(3);
	message_dialog("Ephesos deflects all three blasts, sending them crashing into the walls of the Temple. You hear the walls begin to collapse.","You hope Ephesos can wrap this up quickly...");
	play_sound(108);

	set_character_pose(6,0);
	force_instant_terrain_redraw();
	pause(10);
	text_bubble_on_char(7,"You have failed, Kharprev.");
	force_instant_terrain_redraw();
	pause(20);
	erase_text_bubbles();
	text_bubble_on_char(7,"The Order of Krell has won.");
	force_instant_terrain_redraw();
	pause(15);
	message_dialog("Ephesos begins muttering a spell, and focuses his gaze on Kharprev...","");
	set_character_pose(7,1);
	erase_text_bubbles();
	text_bubble_on_char(6,"NOOOO!!!");
	force_instant_terrain_redraw();
	pause(3);
	put_effect_on_char(7,13,4,1);
	run_animation_sound(163);
	pause(3);
	put_effect_on_char(7,13,10,1);
	run_animation_sound(163);
	pause(3);
	put_effect_on_char(7,13,16,1);
	run_animation_sound(163);
	put_jagged_zap(16,11,16,15,3);
	put_effect_on_char(6,5,16,0);
	run_animation_sound(102);
	pause(5);
	message_dialog("The spell finds its mark and strikes Kharprev's skull. As he begins to fall, he utters one final curse.","_IF I MUST DIE, SO MUST YOU!! MY TEMPLE WILL CRUMBLE WITH YOU IN IT!!!_ he screams with rage, sending beams of fire into every wall of the Temple...");
	pause(3);
	set_character_pose(6,1);
	force_instant_terrain_redraw();
	put_jagged_zap(16,15,0,0,0);
	put_jagged_zap(16,15,0,15,0);
	put_jagged_zap(16,15,0,31,0);
	put_jagged_zap(16,15,15,31,0);
	put_jagged_zap(16,15,31,31,0);
	put_jagged_zap(16,15,31,15,0);
	put_jagged_zap(16,15,31,0,0);
	put_jagged_zap(16,15,15,0,0);
	put_boom_on_char(6,1,0);
	set_terrain(17,15,138);
	set_terrain(16,15,137);
	set_terrain(15,15,138);
	run_animation_sound(152);
	force_instant_terrain_redraw();
	kill_char(6,2,0);
	award_party_xp(1000,30);
	set_state_continue(40);
break;

beginstate 40; //Portal setup
	set_character_pose(7,0);
	pause(5);
	set_flag(11,10,1);
	message_dialog("Kharprev lies dead, his body thoroughly scarred by Ephesos's spell. Ephesos beckons you over, yelling for you to move quickly.","_Quick! We must escape before the Temple crumbles!_ he yells. As one of the walls begins to give way, you anxiously listen to him.");
	force_instant_terrain_redraw();
	reset_dialog();
	add_dialog_str(0,"_Thank you for giving Brother Oakleaf the time he needed to save me._ Ephesos says, while starting to cast a spell. _I could never have escaped alone._",0);
	add_dialog_str(1,"_In any case, give me one more minute, and we'll be out of here._",0);
	add_dialog_str(2, "Ephesos continues the spell, and you suddenly notice that the entire Temple is dissolving around you. The walls are buckling from the damage Kharprev inflicted, and look like they won't hold much longer.",0);
	add_dialog_str(3, "With one last muttered syllable, Ephesos completes the spell, and the Temple is filled with an immense blue bolt of light...",0);
	run_dialog(1);
	pause(2);
	set_character_pose(7,1);
	force_instant_terrain_redraw();
	pause(2);
	put_boom_on_space(16,15,5,0);
	put_boom_on_space(18,15,3,0);
	put_boom_on_space(14,15,3,0);
	put_boom_on_space(16,13,3,0);
	put_boom_on_space(16,17,3,0);
	run_animation_sound(163);
	set_terrain(16,15,268);
	force_instant_terrain_redraw();
	pause(5);
	erase_text_bubbles();
	force_instant_terrain_redraw();
	text_bubble_on_char(7, "Follow me!");
	force_instant_terrain_redraw();
	pause(5);
	set_character_pose(7,0);
	relocate_character(7,16,12);
	force_instant_terrain_redraw();
	pause(5);
	relocate_character(7,16,13);
	force_instant_terrain_redraw();
	pause(5);
	relocate_character(7,16,14);
	force_instant_terrain_redraw();
	pause(5);
	erase_text_bubbles();
	put_boom_on_char(7,2,0);
	run_animation_sound(95);
	erase_char(7);
	set_total_visibility(0);
	force_instant_terrain_redraw();
break;

beginstate 50; //Teleport back to upper Temple
	if(get_flag(11,10) == 1) {
		reset_dialog();
		message_dialog("As you step into the portal, you can feel your bodies being ripped apart, and soon find yourselves reassembled elsewhere...","The portal closes behind you, and the Temple of Flame collapses at last, the deafening sound echoing into the mountains.");
		put_boom_on_char(0,2,0);
		put_boom_on_char(1,2,0);
		put_boom_on_char(2,2,0);
		put_boom_on_char(3,2,0);
		run_animation_sound(95);
		pause(1);
		move_to_new_town(4,12,22);
	}
break;